<?php
namespace app\supplier\controller;

use app\erp\model\Sitedb;
use app\erp\model\Categorydb;
use think\Db;
use app\erp\model\CategoryBranddb;
use app\erp\model\Attrdb;
use app\erp\model\Goodsdb;

class Category extends Base
{
    // 一级分类th
    protected $cols = array(
        array(
            'field' => 'category_icon',
            'width' => 62,
            'title' => '  '
        ),
        array(
            'field' => 'category_id',
            'width' => 100,
            'title' => '编号'
        ),
        array(
            'field' => 'category_sign',
            'width' => 300,
            'title' => '识别码'
        ),
        array(
            'field' => 'category_name',
            'width' => 300,
            'title' => '分类名称',
            'edit' => true
        ),
        array(
            'field' => 'initails',
            'width' => 100,
            'title' => '英文缩写',
            'edit' => true
        ),
        array(
            'field' => 'site_rate',
            'width' => 100,
            'title' => '折扣率%',
            'edit' => true
        ),
        array(
            'field' => 'api_option',
            'width' => 120,
            'title' => '接口参数',
            'edit' => true
        ),
        array(
            'field' => 'sort',
            'width' => 80,
            'title' => '排序',
            'edit' => true
        ),
        array(
            'field' => 'audit_state',
            'width' => 80,
            'title' => '审核',
            'templet' => '#audit_stateTpl'
        )
    );
    // 二级分类th
    protected $cols_son = array(
        array(
            'field' => 'category_icon',
            'width' => 56,
            'title' => '  '
        ),
        array(
            'field' => 'category_id',
            'width' => 100,
            'title' => ' '
        ),
        array(
            'field' => 'category_sign',
            'width' => 300,
            'title' => '识别码'
        ),
        array(
            'field' => 'category_name',
            'width' => 300,
            'title' => 'loading...',
            'edit' => true
        ),
        array(
            'field' => 'initails',
            'width' => 100,
            'title' => ' ',
            'edit' => true
        ),
        array(
            'field' => 'site_rate',
            'width' => 100,
            'title' => '折扣率',
            'edit' => true
        ),
        array(
            'field' => 'api_option',
            'width' => 120,
            'title' => '第三方参数',
            'edit' => true
        ),
        array(
            'field' => 'sort',
            'width' => 80,
            'title' => ' ',
            'edit' => true
        ),
        array(
            'field' => 'audit_state',
            'width' => 80,
            'title' => '审核',
            'templet' => '#audit_stateTpl'
        )
    );
    // 三级分类th
    protected $cols_end = array(
        array(
            'field' => 'category_icon',
            'width' => 50,
            'title' => '  '
        ),
        array(
            'field' => 'category_id',
            'width' => 100,
            'title' => ' '
        ),
        array(
            'field' => 'category_sign',
            'width' => 300,
            'title' => '识别码'
        ),
        array(
            'field' => 'category_name',
            'width' => 300,
            'title' => 'loading...',
            'edit' => true
        ),
        array(
            'field' => 'initails',
            'width' => 100,
            'title' => ' ',
            'edit' => true
        ),
        array(
            'field' => 'site_rate',
            'width' => 100,
            'title' => '折扣率',
            'edit' => true
        ),
        array(
            'field' => 'api_option',
            'width' => 120,
            'title' => '第三方参数',
            'edit' => true
        ),
        array(
            'field' => 'sort',
            'width' => 80,
            'title' => ' ',
            'edit' => true
        ),
        array(
            'field' => 'audit_state',
            'width' => 80,
            'title' => '审核',
            'templet' => '#audit_stateTpl'
        )
    );
    // 分类属性th
    protected $cols_attr = array(
        array(
            'field' => 'sort',
            'width' => 60,
            'title' => '排序',
            'edit' => true
        ),
        array(
            'field' => 'group_name',
            'width' => 120,
            'title' => '参数分组',
            'edit' => true
        ),
        array(
            'field' => 'attr_name',
            'width' => 120,
            'title' => '属性名称',
            'edit' => true
        ),
        array(
            'field' => 'is_must',
            'width' => 120,
            'title' => '是否必填',
            'templet' => '#is_mustTpl'
        ),
        array(
            'field' => 'value_type',
            'width' => 100,
            'title' => '类型',
            'templet' => '#value_typeTpl'
        ),
        array(
            'field' => 'select_type',
            'width' => 100,
            'title' => '选择类型',
            'templet' => '#select_typeTpl'
        ),
        array(
            'field' => 'value_item',
            'width' => 300,
            'title' => '可选值（","分割）',
            'edit' => true
        ),
        array(
            'field' => 'unit',
            'width' => 80,
            'title' => '单位',
            'edit' => true
        ),
        array(
            'width' => 120,
            'toolbar' => '#attr_tool',
            'title' => '操作'
        )
    );

    public $post;

    public $db;

    public function _initialize()
    {
        parent::_initialize();
        $request = request();
        $param = $request->param();
        $this->post = $param;
        if (isset($this->post['_o'])) {
            $this->post['_o'] = str_replace('_text', '', $this->post['_o']);
        }
        // 调用分站数据
        $sitedb = new Sitedb();
        $sitedb->param = array(
            '_o' => 'site_id',
            '_b' => 'asc',
            'page' => 1,
            'limit' => 1000
        );
        $get_site_list = $sitedb->ajaxSelect();
        $this->db = new Categorydb();
        $this->assign('get_site_list', $get_site_list['data']);
    }

    public function index()
    {
        array_push($this->cols, array(
            'width' => 134,
            'toolbar' => '#category_tool'
        ));
        array_push($this->cols_son, array(
            'width' => 132,
            'toolbar' => '#category_tool'
        ));
        array_push($this->cols_end, array(
            'width' => 130,
            'toolbar' => '#category_tool_end'
        ));
        
        // 获取分站信息
        $sitedb = new Sitedb();
        $get_site = $sitedb->ajaxFind(SITE_ID);
        $this->assign('get_site', $get_site);
        // 获取分站列表
        $sitedb->param['_o'] = 'site_id';
        $sitedb->param['_b'] = 'asc';
        $sitedb->param['page'] = 1;
        $sitedb->param['limit'] = 1000;
        $get_site_list = $sitedb->ajaxSelect();
        if ($get_site['site_name'] != '主站') {
            for ($i = 0; $i < count($this->cols); $i ++) {
                if (isset($this->cols[$i]['field']) && $this->cols[$i]['field'] == 'site_rate') {
                    unset($this->cols[$i]['edit']);
                }
            }
            for ($i = 0; $i < count($this->cols_son); $i ++) {
                if (isset($this->cols_son[$i]['field']) && $this->cols_son[$i]['field'] == 'site_rate') {
                    unset($this->cols_son[$i]['edit']);
                }
            }
            for ($i = 0; $i < count($this->cols_end); $i ++) {
                if (isset($this->cols_end[$i]['field']) && $this->cols_end[$i]['field'] == 'site_rate') {
                    unset($this->cols_end[$i]['edit']);
                }
            }
        }
        $this->assign('cols', json_encode($this->cols));
        $this->assign('cols_son', json_encode($this->cols_son));
        $this->assign('cols_end', json_encode($this->cols_end));
        $this->assign('site_list', $get_site_list['data']);
        $this->assign('site_id',SITE_ID);
        return $this->fetch();
    }
    // 同步分类
    public function copy($select_site_id = '')
    {
        $categorydb = new Categorydb();
        $category_all_sign = $categorydb->ajaxAllSign($select_site_id);
        $category_sign_arr = array();
        foreach ($category_all_sign as $key => $val) {
            array_push($category_sign_arr, $val['category_sign']);
        }
        if ($select_site_id != '') {
            $categorydb->param['site_id'] = 0;
        }
        
        $category_info = $categorydb->ajaxSelect();
        $return = $category_info['data'];
        foreach ($return as $key => $val) {
            $category_info_son = $categorydb->ajaxSelect($val['category_sign']);
            $return[$key]['_'] = $category_info_son['data'];
            foreach ($return[$key]['_'] as $k => $v) {
                $category_info_three = $categorydb->ajaxSelect($v['category_sign']);
                $return[$key]['_'][$k]['_'] = $category_info_three['data'];
            }
        }
        $this->assign('category_sign_arr', $category_sign_arr);
        $this->assign('select_site_id', $select_site_id);
        $this->assign('category_info', $return);
        return $this->fetch();
    }

    public function saveCopy()
    {
        if (! isset($this->post['category_sign']) || count($this->post['category_sign']) == 0) {
            $this->layuiReturn('请选择要同步的分类');
        }
        if (SITE_ID == null && $this->post['select_site_id'] == '') {
            $this->layuiReturn('请选择要同步的分站');
        }
        set_time_limit(800);
        Db::startTrans();
        try {
            if ($this->post['select_site_id'] == '') {
                $this->post['copy_product'] = 1;
                goto copyProduct;
            }
            // 同步分类
            $categorydb = new Categorydb();
            $categorydb->param = $this->post;
            $categorydb->param['site_id'] = 0;
            $categorydb->copy();
            
            // 同步分类品牌
            $categoryBranddb = new CategoryBranddb();
            $categoryBranddb->param['site_id'] = 0;
            $categoryBranddb->param['category_sign'] = $this->post['category_sign'];
            $categoryBranddb->param['select_site_id'] = $this->post['select_site_id'];
            $categoryBranddb->copy();
            // 同步属性
            $attrdb = new Attrdb();
            $attrdb->param['site_id'] = 0;
            $attrdb->param['category_sign'] = $this->post['category_sign'];
            $attrdb->param['select_site_id'] = $this->post['select_site_id'];
            $attrdb->copy();
            copyProduct:
            // 同步商品
            if (isset($this->post['copy_product']) && $this->post['copy_product'] == 1) {
                $goodsdb = new Goodsdb();
                $goodsdb->param['site_id'] = 0;
                $goodsdb->param['category_sign'] = $this->post['category_sign'];
                $goodsdb->param['select_site_id'] = $this->post['select_site_id'];
                $goodsdb->param['sale_state'] = 1;
                $goodsdb->param['is_copy'] = 1;
                $goodsdb->param['audit_state'] = 1;
                $goodsdb->copy();
            }
            Db::commit();
            $this->layuiReturn('数据同步成功', '', '数据同步成功', 1);
        } catch (\Exception $e) {
            $this->layuiReturn('数据同步失败，请联系管理员');
            Db::rollback();
        }
    }

    public function tplForm()
    {
        $categorydb = new Categorydb();
        $category_sign = 0;
        $data = array();
        if (isset($this->post['category_sign'])) {
            $data = $categorydb->ajaxFind($this->post['category_sign']);
            $category_sign = $this->post['category_sign'];
            // $this->assign('get_category',$get_category);
        } elseif (isset($this->post['parent_sign'])) {
            $data['parent_sign'] = $this->post['parent_sign'];
        }
        $this->assign('data', json_encode($data));
        // 调用1,2级分类
        $this->assign('category_sign', $category_sign);
        unset($categorydb->param['category_sign']);
        $category_info = $categorydb->ajaxSelect();
        $return = $category_info['data'];
        foreach ($return as $key => $val) {
            $category_info_son = $categorydb->ajaxSelect($val['category_sign']);
            $return[$key]['_'] = $category_info_son['data'];
        }
        // 获取分站信息
        $sitedb = new Sitedb();
        $get_site = $sitedb->ajaxFind(SITE_ID);
        $this->assign('get_site', $get_site);
        $this->assign('category_info', $return);
        $this->assign('cols_attr', json_encode($this->cols_attr));
        return $this->fetch();
    }

    /**
     *
     * @todo 保存数据
     */
    public function saveData()
    {
        $categorydb = new Categorydb();
        $categorydb->param = $this->post;
        $return = $categorydb->ajaxSave();
        if ($return != 'success') {
            $this->layuiReturn($return, 0, $return, 0);
        } else {
            $data['parent_sign'] = $this->post['parent_sign'];
            if ($data['parent_sign'] != '0') {
                $categorydb->param = array();
                $category_info = $categorydb->ajaxFind($this->post['parent_sign']);
                $data['top_id'] = $category_info['parent_sign'];
                //$data['parent_sign'] = $category_info['category_id'];
            }
            $this->layuiReturn($data, '', '修改成功', 1);
        }
    }

    public function saveSiteData()
    {
        $categorydb = new Categorydb();
        $categorydb->param[$this->post['field']] = $this->post['value'];
        $categorydb->param['site_id'] = $this->post['site_id'];
        $categorydb->param['category_sign'] = $this->post['category_sign'];
        $return = $categorydb->ajaxUpdate();
        if ($return != 'success') {
            $this->layuiReturn($return, 0, $return, 0);
        } else {
            $data['parent_sign'] = $this->post['parent_sign'];
            if ($data['parent_sign'] != '0') {
                $categorydb->param = array();
                $category_info = $categorydb->ajaxFind($this->post['parent_sign']);
                $data['top_id'] = $category_info['parent_sign'];
                //$data['parent_sign'] = $category_info['category_id'];
            }
            $this->layuiReturn($data, '', '保存成功', 1);
        }
    }

    /**
     *
     * @todo 软删除
     */
    public function ajaxDelete()
    {
        $categorydb = new Categorydb();
        $categorydb->param = $this->post;
        $categorydb->ajaxDelete();
        $this->layuiReturn('删除成功', 0);
    }

    public function ajaxCategory()
    {
        $categorydb = new Categorydb();
        $categorydb->param = $this->post;
        // dump($categorydb->param);
        $return = $categorydb->ajaxSelect();
        $this->layuiReturn($return['data'], $return['count']);
    }

    public function siteData()
    {
        if (! isset($this->post['category_sign']) || ! isset($this->post['parent_sign'])) {
            exit('参数错误');
        }
        $this->db->param['site_id'] = - 1;
        $this->db->param['category_sign'] = $this->post['category_sign'];
        $data = $this->db->ajaxList();
        $this->assign('data', $data);
        $this->assign('parent_sign', $this->post['parent_sign']);
        return $this->fetch();
    }
}